﻿using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Eneta.Portal.Controls
{
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class LeftMenuControl : HierarchicalDataBoundControl
    {

        public virtual string MenuClass { get; set; }

        /// <summary>
        /// Renders begin tag of menu element.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            if(string.IsNullOrEmpty(MenuClass))
                writer.Write("<ul>");
            else
                writer.Write("<ul class=\"" + MenuClass + "\">");
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            EnsureDataBound();

            var source = (SiteMapDataSource)GetDataSource();
            var provider = source.Provider;

            if (provider == null)
            {
                output.Write("&nbsp;");
                return;
            }
            
            if (provider.RootNode == null)
            {
                output.Write("&nbsp;");
                return;
            }
            if (provider.RootNode.ChildNodes.Count == 0)
            {
                output.Write("&nbsp;");
                return;
            }

            var node = provider.CurrentNode;            
            while(true)
            {
                if (node.ParentNode.ParentNode == null)
                    break;
                node = node.ParentNode;
            }

            foreach (SiteMapNode childNode in node.ChildNodes)
            {
                
                output.Write("<li");
                if (childNode.Equals(provider.CurrentNode))
                    output.Write(" class=\"active\"");
                output.Write("><a href=\"" + childNode.Url + "\">");
                output.Write(childNode.Title);
                output.Write("</a>");

                if (childNode.ChildNodes.Count > 0)
                {
                    output.Write("<ul class=\"subnav\">");
                    foreach (SiteMapNode childChildNode in childNode.ChildNodes)
                    {
                        output.Write("<li");
                        if (childChildNode.Equals(provider.CurrentNode))
                            output.Write(" class=\"active\"");
                        output.Write("><a href=\"" + childChildNode.Url + "\">");
                        output.Write(childChildNode.Title);
                        output.Write("</a>");
                        output.Write("</li>");
                    }
                    output.Write("</ul>");
                }

                output.Write("</li>");
            }
        }

        /// <summary>
        /// Renders the closing tag of menu.
        /// </summary>
        /// <param name="writer">HtmlTextWriter that accepts menu control output.</param>
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.Write("</ul>");
        }
    }
}
